package com.elective.courseselect.controller;

import com.alibaba.fastjson.JSONObject;
import com.elective.courseselect.entity.CourseListVo;
import com.elective.courseselect.entity.StudentEntity;
import com.elective.courseselect.service.StudentService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
 /**
 * 
 *
 * @author lee
 * @email 943204006@qq.ccom
 * @date 2021-09-04 19:22:34
 */
@RestController
@RequestMapping("courseselect/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params ,  StudentEntity studentEntity){
        PageUtils page = studentService.queryPage(params,studentEntity);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		StudentEntity student = studentService.getById(id);
        return R.ok().put("student", student);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody StudentEntity student){
		studentService.save(student);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody StudentEntity student){
		studentService.updateById(student);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		studentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }




     /**
      * 判断学生是否已经选择该门课程
      */
     @ApiOperation("判断是否已选课（answer去查， 以及 student_course）")
     @GetMapping("/judgeStuChooseCourseByCourseId/{userId}/{courseId}")
     public R judgeStuChooseCourseByCourseId(@PathVariable("userId") Integer userId , @PathVariable("courseId")Integer courseId){
         Boolean bol = studentService.judgeStuChooseCourseByCourseId(userId,courseId);
         return R.ok(StatusCode.OK,bol == true?"已选":"未选").put("isChoose", bol);
     }


     /**
      * 抢课
      */
     @ApiOperation("抢课")
     @GetMapping("/fightCourse/{userId}/{courseId}")
     public R fightCourse(@PathVariable("userId") Integer userId , @PathVariable("courseId")Integer courseId){
         return studentService.fightCourse(userId,courseId);
     }

     /**
      * 查看选课结果
      */
     @ApiOperation("查看选课结果")
     @GetMapping("/searchChooseCourseRes/{userId}")
     public R searchChooseCourseRes(@PathVariable("userId") Integer userId ){
         CourseListVo list =  studentService.searchChooseCourseRes(userId);
         return R.ok(StatusCode.OK,"查看选课结果成功").put("list", list);
     }


     /**
      * 查看选课结果
      */
     @ApiOperation("查看选课结果 根据课程ID排序")
     @GetMapping("/searchChooseCourseResTypeByCourseId/{userId}")
     public R searchChooseCourseResTypeByCourseId(@PathVariable("userId") Integer userId ){
         JSONObject obj = studentService.searchChooseCourseResTypeByCourseId(userId);
         return R.ok(StatusCode.OK,"查看选课结果成功").put("list", obj);
     }


     /**
      * 取消选课
      */
     @ApiOperation("取消选课")
     @GetMapping("/cancelChooseCourse/{courseId}/{userId}")
     public R cancelChooseCourse(@PathVariable("courseId") Integer courseId ,
                                 @PathVariable("userId") Integer userId){
         R r = studentService.cancelChooseCourse(courseId,userId);
         return r;
     }
 }
